from django.shortcuts import render_to_response, redirect

from django.template import RequestContext

from Prova.models import Prova, ProvaForm, Trabalho, TrabalhoForm, MateriaForm

# Create your views here.
def index(request):     
    provas = Prova.objects.order_by('data') 
    trabalhos = Trabalho.objects.order_by('data')   
    return render_to_response('index.html', dict(provas=provas, trabalhos=trabalhos))
    

def prova(request, id):
    return render_to_response('cadastra.html', dict(formset=ProvaForm(instance=Prova.objects.get(pk=id))))
    
def trabalho(request, id):
    return render_to_response('cadastra.html', dict(formset=TrabalhoForm(instance=Trabalho.objects.get(pk=id))))
      
def cadastraProva(request):
    if request.method == 'POST':
        form = ProvaForm(request.POST)
        if form.is_valid():
            form.save()
            redirect('compromissos/')
    else :
        form = ProvaForm()
        
    dicio = dict(formset=form, path=request.META['PATH_INFO'])
    return render_to_response('cadastra.html', dicio, context_instance=RequestContext(request))
    
def cadastraTrabalho(request):
    if request.method == 'POST':
        form = TrabalhoForm(request.POST)
        if form.is_valid():
            form.save()
            redirect('compromissos/')
    else :
        form = TrabalhoForm()
    return render_to_response('cadastra.html', dict(formset=form, path=request.META['PATH_INFO']), context_instance=RequestContext(request))

def cadastraMateria(request):
    if request.method == 'POST':
        form = MateriaForm(request.POST)
        if form.is_valid():
            form.save()
            redirect('compromissos/')
    return render_to_response('cadastra.html', dict(formset=MateriaForm, path=request.META['PATH_INFO']), context_instance=RequestContext(request))
